﻿using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;

namespace CDude.Vita
{
    internal class Language
    {
        private static readonly Version APP_VERSION = Assembly.GetExecutingAssembly().GetName().Version;
        private static readonly string[] compatiblelangversions = new string[] { "1.12" };
        public LangInfo Info = new LangInfo();
        public LangStrings Strings = new LangStrings();

        public Language(string path)
        {
            using (XmlTextReader xmlreader = new XmlTextReader(path))
            {
                string nodename = null;
                bool info = true;
                try
                {
                    while (xmlreader.Read())
                    {
                        switch (xmlreader.NodeType)
                        {
                            case XmlNodeType.Element:
                                nodename = xmlreader.Name;
                                break;

                            case XmlNodeType.Text:
                                try
                                {
                                    if (info)
                                    {
                                        if (nodename == "Version")
                                        {
                                            if (!compatiblelangversions.Contains(xmlreader.Value))
                                            {
                                                MessageBox.Show(Path.GetFileNameWithoutExtension(path) + " is only compatible with VitaCOMgui " + xmlreader.Value, "Error – VitaCOMgui", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                                throw new Exception();
                                            }
                                        }
                                        FieldInfo infofield = Info.GetType().GetField(nodename);
                                        infofield.SetValue(Info, xmlreader.Value);
                                    }
                                    else
                                    {
                                        FieldInfo langfield = Strings.GetType().GetField(nodename);
                                        langfield.SetValue(Strings, xmlreader.Value);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show(Path.GetFileNameWithoutExtension(path) + ": Invalid field: " + nodename, "Warning – VitaCOMgui", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                    throw ex;
                                }
                                break;

                            case XmlNodeType.EndElement:
                                if (xmlreader.Name == "Info") info = false;
                                break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Bad XML: " + ex.Message);
                    throw ex;
                }
            }
        }

        internal class LangInfo
        {
            public string Author;
            public string Code;
            public string Language;
            public string LLanguage;
            public string Version;
        }

        internal class LangStrings
        {
            public string App_name;
            public string Apply;
            public string Arguments;
            public string CannotSaveThere;
            public string Close;
            public string ClosedPSM;
            public string Connect;
            public string Connected;
            public string Connecting;
            public string ConnectOffline;
            public string CouldNotConnect;
            public string CouldNotDownloadText;
            public string Create_Package;
            public string Debug;
            public string DeleteAllApps;
            public string DeleteButtonMessage;
            public string Deleted;
            public string Deleting;
            public string DeletionPackageNotFound;
            public string Desired_app_name;
            public string Device;
            public string Disconnect;
            public string Disconnected;
            public string Display_output;
            public string DownloadingText;
            public string Encryption_type;
            public string Error;
            public string GettingDevList;
            public string GotDevList;
            public string INSTALL;
            public string Install_Application;
            public string Installed;
            public string Installing;
            public string Keep_device_connection;
            public string Kill_PSM;
            public string Language;
            public string LAUNCH;
            public string CONVERT;
            public string Launch_Application;
            public string Launched;
            public string Launching;
            public string LostConnection;
            public string Maximize;
            public string Minimize;
            public string Move;
            public string NoDevsConnected;
            public string NoEXE;
            public string None;
            public string offline;
            public string Switch_to_converting;
            public string Switch_to_packing;
            public string Convert_Package;
            public string Select_input_file;
            public string NoILTools;
            public string online;
            public string OpInProgress;
            public string OpNotCompleted;
            public string PACK;
            public string Packed;
            public string Packing;
            public string Profile;
            public string PSM_Application_Output;
            public string Refresh;
            public string Restore;
            public string Select_File;
            public string Select_input_folder;
            public string Select_output_file;
            public string SelectAppFolder;
            public string SelectLanguage;
            public string SelectPackage;
            public string Size;
            public string TypeOutputFile;
            public string UNINSTALL;
            public string Uninstall_Application;
            public string Uninstalled;
            public string Uninstalling;
            public string Warning;
        }
    }
}